"""
   # File : ExcelControl.py
   # Time : 2021/12/13
   # Author : XGT
   # Version : V1.0.0
   # Description : Web数据驱动制作,读取测试数据,写入测试结果
"""


import openpyxl
from Log.logger import Log
from openpyxl.styles import PatternFill, Font



def write_Test_Data(excel_path):
    """
       :excel_path: 需要读取excel表格的路径
       :return: 返回测试数据,用于测试
       :note: Sheet1: 表示excel中的子表,可自行修改
              存储方式: list[list[dict]]
              后续如果需要多子表的数据,可自行进行修改
              方法:遍历子表,存储数据,这里暂不对应，后续更新
    """
    excel = openpyxl.load_workbook(excel_path)
    sheet = excel['Sheet1']
    Log.info('获取路径{0}文件的{1}内容成功,现在开始执行自动化测试------>'.format(excel_path,sheet))
    this_excel_data = []
    try:
        for i in range(2, sheet.max_row + 1):
            args = {}
            args['event'] = sheet.cell(i,2).value
            args['name'] = sheet.cell(i, 3).value
            args['value'] = sheet.cell(i,4).value
            args['txt'] = sheet.cell(i,5).value
            args['Des'] = sheet.cell(i,6).value
            args['except'] = sheet.cell(i,7).value
            this_excel_data.append(args)
        this_excel_data.append(excel_path)
        return this_excel_data
    except Exception as e:
        Log.error("读取"+excel_path+"表数据失败"+str(e))

def write_Test_Result(excel_path, Res, j, staut):
    """
       :excel_path: 需要读取excel表格的路径
       :Res: 实际结果
       :j: 测试用例在表格中的行数,用于写入
       :staut: 测试用例运行结果
    """

    excel = openpyxl.load_workbook(excel_path)
    sheet = excel['Sheet1']
    if staut == 'OK':
        sheet.cell(row=j+2, column=8).value = Res
        sheet.cell(row=j+2, column=9).value = 'Pass'
        sheet.cell(row=j+2, column=9).fill = PatternFill("solid", fgColor="00FF00")
        sheet.cell(row=j+2, column=9).font = Font(bold=True)
    else:
        sheet.cell(row=j + 2, column=8).value = Res
        sheet.cell(row=j+2, column=9).value = 'Failed'
        sheet.cell(row=j+2, column=9).fill = PatternFill('solid', fgColor='FF0000')
        sheet.cell(row=j+2, column=9).font = Font(bold=True)
    excel.save(excel_path)

if __name__ == '__main__':
    print(write_Test_Data('../Data1/WebDriver.xlsx'))










